<template>
    <div class="customer-card">
        <CardTitle title="最近关注" @change="onChangeCardTitle" />
        <el-tabs v-model="activeName" class="demo-tabs" @tab-change="onChangeTab">
            <el-tab-pane label="图片" name="2">
                <Carousel v-if="state.imgList && state.imgList.length > 0" type="img" :list="state.imgList" />
                <el-empty v-else image="/static/noData.png" description="暂无数据"></el-empty>
            </el-tab-pane>
            <el-tab-pane label="视频" name="4">
                <Carousel v-if="state.videoList && state.videoList.length > 0" type="video" :list="state.videoList" />
                <el-empty v-else image="/static/noData.png" description="暂无数据"></el-empty>
            </el-tab-pane>
            <el-tab-pane label="链接" name="6">
                <Tags :tags="state.tags" />
                <el-row v-if="state.linkList && state.linkList.length > 0" :gutter="20" class="link-box">
                    <el-col v-for="(item, index) in state.linkList" :key="index" :span="6">
                        <div class="item-box" @click="onOpenPage(item.ref_url)">
                            <div class="link-left text">
                                <p class="link-title omit-text">{{ item.title }}</p>
                                <p class="link-desc omit-text">{{ item.desc }}</p>
                            </div>
                            <el-image :src="item.cover_image_url" fit="cover" class="cover-img">
                                <template #error>
                                    <div class="image-slot">
                                        <el-icon>
                                            <icon-picture />
                                        </el-icon>
                                    </div>
                                </template>
                            </el-image>
                        </div>
                    </el-col>
                </el-row>
                <el-empty v-else image="/static/noData.png" description="暂无数据"></el-empty>
            </el-tab-pane>
            <el-tab-pane label="朋友圈" name="8">
                <el-table :data="state.tableData" stripe border>
                    <el-table-column fixed prop="title" label="朋友圈标题" width="200" />
                    <el-table-column prop="publish_time" label="发布时间" width="140" />
                    <el-table-column prop="name" label="发布员工" />
                    <el-table-column prop="like_nums" label="点赞次数" />
                    <el-table-column prop="comment_nums" label="评论次数" />
                    <el-table-column prop="interaction_time" label="最近一次互动时间" width="140" />
                    <el-table-column fixed="right" label="操作" width="120">
                        <template #default="scope">
                            <el-button size="small" @click="goDetail(scope.row)">详情</el-button>
                        </template>
                    </el-table-column>
                    <template #empty>
                        <el-empty image="/static/noData.png" description="暂无数据"></el-empty>
                    </template>
                </el-table>
                <ZWPagination @submit="onZWPagination" />
            </el-tab-pane>
        </el-tabs>
    </div>
</template>

<script setup lang="ts">
import { defineProps, ref, reactive } from 'vue';
import { useRoute } from 'vue-router';
import { Picture as IconPicture } from '@element-plus/icons-vue';
import { getFollowList, getWXGroup } from '@/api/customerCenter';
import { useStore } from '@/store';

import ZWPagination from '@/components/ZWPagination.vue';
import CardTitle from './CardTitle.vue';
import Tags from './Tags.vue';
import Carousel from './Carousel.vue';
import router from '@/router';
import { getEmojiUrl } from '@/utils/emoji';

const route = useRoute();
const store = useStore();

const activeName = ref('2');
const state = reactive({
    time: '',
    tags: [],
    imgList: [],
    videoList: [],
    linkList: Array<{
        title: string;
        content: string;
        desc: string;
        cover_image_url: string | undefined;
        ref_url: any;
    }>(),
    tableData: []
});
const params = ref({
    cust_id: route.query.id,
    material_type: '',
    select_time: '',
    page_size: 15,
    page: 1
});
//跳转详情
const goDetail = (val: any) => {
    router.push({
        path: '/channel/friendcircle/qwFriendCricle/friendDetail',
        query: { id: val.id, title: val.title }
    });
};
// 获取页面初始化数据
const getData = () => {
    const _i = ['2', '4', '6'].indexOf(activeName.value);
    if (_i != -1) {
        if (_i === 2) {
            params.value.material_type = '6,7';
        } else {
            params.value.material_type = activeName.value;
        }
        getFollowList(params.value).then((res: any) => {
            const t = ['imgList', 'videoList', 'linkList'][_i];
            state[t] = res;
        });
    } else {
        // 朋友圈数据
        getWXGroup(params.value).then((res: any) => {
            state.tableData = res.data;
            store.dispatch('pageTotal', res.total);
        });
    }
};

const onChangeCardTitle = (e: any) => {
    state.time = e.join('~');
    params.value.select_time = e.join('~');
    getData();
};

const onChangeTab = (e: any) => {
    activeName.value = e;
    getData();
};

const onZWPagination = (e: any) => {
    params.value.page = e.page;
    params.value.page_size = e.page_size;
    getData();
};

const onOpenPage = (url: string) => {
    window.open(url);
};
</script>

<style lang="scss" scoped>
.customer-card {
    padding: 16px 20px;

    .link-box {
        margin-top: 10px;

        .el-col {
            padding: 10px;
        }

        .item-box {
            display: flex;
            justify-content: space-between;
            padding: 5px;
            background: #f9f9f9;
            border-radius: 1px;
            border: 1px solid #eaeaea;
            cursor: pointer;

            .text {
                display: flex;
                flex-direction: column;
                justify-content: center;
                width: 100%;
                flex: auto;
                margin-right: 10px;

                p {
                    margin: 0;

                    &:first-of-type {
                        margin-bottom: 6px;
                        font-size: 14px;
                        font-weight: 500;
                        color: #33302d;
                    }

                    &:last-of-type {
                        font-size: 12px;
                        font-weight: 300;
                        color: #969696;
                        -webkit-line-clamp: 2;
                    }
                }

                .omit-text {
                    // 显示两行文字，多余文字省略
                    text-overflow: ellipsis;
                    overflow: hidden;
                    -webkit-line-clamp: 2;
                    -webkit-box-orient: vertical;
                    display: -moz-box;
                    -moz-line-clamp: 2;
                    -moz-box-orient: vertical;
                    word-wrap: break-word;
                    word-break: break-all;
                    white-space: normal;
                }
            }

            .cover-img {
                display: flex;
                justify-content: center;
                align-items: center;
                width: 60px;
                height: 60px;
                vertical-align: middle;
                object-fit: cover;

                .image-slot {
                    .el-icon {
                        font-size: 36px;
                    }
                }
            }
        }
    }
}

.firstText {
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
</style>
